package com.example.data.factory.web1.controller;

import com.example.data.factory.common.Dto.*;
import com.example.data.factory.common.result.R;
import com.example.data.factory.entity.entity.Interface;
import com.example.data.factory.service.service.InterfaceService;
import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiModelProperty;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.List;
import java.util.Map;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author lhy
 * @since 2025-03-03
 */
@RestController
@RequestMapping("/interface")
@ApiModel("接口管理相关接口")
public class InterfaceController {
    @Resource
    private InterfaceService interfaceService;

    @ApiModelProperty("接口分页查询接口")
    @GetMapping("/SearchInterface")
    public R<Interface> searchInterface(@ModelAttribute SearchInterfaceDTO searchInterfaceDTO) {
        return interfaceService.search ( searchInterfaceDTO );

    }

    @PostMapping("/InsertInterface")
    public R<Interface> insertinterface(@RequestBody @Validated InsertInterfaceDto insertInterfaceDto, HttpServletRequest request) {
        return interfaceService.insert ( insertInterfaceDto, request );
    }

    @PostMapping("/InsertFaceAndInterFaceParam")
    public R<Interface> InsertFaceAndInterFaceParam(@RequestBody InsertInterfaceAndInterfaceParamDto insertInterfaceAndInterfaceParamDto, HttpServletRequest request) {
        return interfaceService.insets ( insertInterfaceAndInterfaceParamDto, request );
    }

    @PutMapping("/UpdateInterfaceAndInterfaceParam")
    public R<Interface> UpdateInterfaceAndInterfaceParam(@RequestBody UpdateInterfaceAndInterfaceParamDto updateInterfaceAndInterfaceParamDto) {
        return interfaceService.updates ( updateInterfaceAndInterfaceParamDto );
    }
    @PutMapping("/UpdateInterfaceStatus")
    public R<Interface> UpdateInterfaceStatus (@RequestBody UpdateInterfaceStatusDto updateInterfaceStatusDto ) {
        return interfaceService.updatestatus(updateInterfaceStatusDto);
    }
    @PutMapping("/UpdateInterfaceStatusBatch")
    public R<Interface> UpdateInterfaceStatusBatch (@RequestBody List<UpdateInterfaceStatusDto> updateInterfaceStatusDtos) {
        return interfaceService.updatestatsbatch(updateInterfaceStatusDtos);
    }
    @DeleteMapping("/DeleteInterface")
    public R<Interface> DeleteInterface (@RequestBody DeleteInterfaceDto deleteInterfaceDto) {
        return interfaceService.delete ( deleteInterfaceDto );
    }

    @GetMapping("/SearchInterfaceDetail")
    public R<Interface> SearchInterfaceDetail(DeleteInterfaceDto deleteInterfaceDto) {
        return interfaceService.searchdetail ( deleteInterfaceDto );
    }

    @PostMapping("BatachClassify")
    public R<Map<String, String>> BatachClassify(@RequestBody ClassifyBatch request) {
        return interfaceService.classifyBatch ( request );
    }

    @GetMapping("/params/{id}")
    public R<Map<String, String>> testInterfaces(@PathVariable Integer id) {
        Map<String, String> requiredParams = interfaceService.testInterfaces ( id );
        return R.Success ( requiredParams );

    }

    /**
     * 测试接口
     */
    @PostMapping("/run/{id}")
    public R<String> runTest(@PathVariable Integer id,
                             @RequestBody Map<String, String> userParams) {
        try {
            // 调用服务层执行接口测试
            String response = interfaceService.testInterfaceById ( id, userParams );
            return R.Success ( response ); // 返回成功响应
        } catch (Exception e) {
            return R.Failed ( e.getMessage () ); // 返回错误响应
        }
    }
}
